#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Magpie - an open source discussion forum system.
#
# Copyright (C) 2009 The Magpie Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ajiaojr.commons.appengine import counter
from ajiaojr.commons.django.shortcuts import render_to_response

from ajiaojr.magpie import forms, models
from ajiaojr.magpie.views import build_template_params
from ajiaojr.magpie.views import club_enabled_required
from ajiaojr.magpie.views import club_writable_required
from ajiaojr.magpie.views import pm_enabled_required
from ajiaojr.magpie.views import pm_required
from ajiaojr.magpie.views import pm_writable_required

from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _

@club_writable_required
@pm_writable_required
@pm_required
def delete(request, message_id):
  request.pm.is_deleted = True;
  request.pm.put()
  return HttpResponseRedirect('/pm/inbox/')

@club_enabled_required
@pm_enabled_required
def inbox(request, starting_index = None):
  if starting_index == '':
    starting_index = None
    
  p = build_template_params(_('Inbox'),
                              request.get_full_path(),
                              request.user)
  
  pms, next = request.user.get_received_messages(starting_index = starting_index)
  
  if next is not None:
    p['next_index'] = next
  
  p['messages'] = pms
  return render_to_response(request, 'message/inbox.html', p)

@club_enabled_required
@pm_enabled_required
@pm_required
def read(request, message_id):
  pm = request.pm
  
  if not pm.is_read:
    pm.is_read = True
    pm.put()
    
  p = build_template_params(_('Message Details'),
                            request.get_full_path(),
                            request.user)
  p['pm'] = pm
  return render_to_response(request, 'message/read.html', p)

@club_writable_required
@pm_writable_required
@pm_required
def reply(request, message_id):
  if request.method == 'POST':
    f = forms.PrivateMessageForm(request.POST)
    if f.is_valid():
      pm = f.save(commit=False)
      pm.message_index = str(pm.message_time)+'|'+pm.sender.key().name()
      pm.put()
      return HttpResponseRedirect('/right.php')
  else:
    f = forms.PrivateMessageForm()
      
  f.fields['sender'].initial = request.pm.recipient.key().name()
  f.fields['recipient'].initial = request.pm.sender.key().name()
  if not request.pm.title.startswith('Re: '):
    f.fields['title'].initial = 'Re: ' + request.pm.title
    
  p = build_template_params(_('Reply'), request.get_full_path(), request.user)
  p['form'] = f
  return render_to_response(request, 'message/info.html', p)

@club_writable_required
@pm_writable_required
def send(request, user_key_name):
  if request.method == 'POST':
    f = forms.PrivateMessageForm(request.POST)
    if f.is_valid():
      pm = f.save(commit=False)
      pm.message_index = str(pm.message_time)+'|'+pm.sender.key().name()
      pm.put()
      return HttpResponseRedirect('/right.php')
  else:
    f = forms.PrivateMessageForm()
    
  f.fields['sender'].initial = request.user.key().name()
  if user_key_name is not None or user_key_name != '':
    f.fields['recipient'].initial = models.ClubUser.get_by_key_name(user_key_name).key().name()
  
  p = build_template_params(_('Send Private Message'),
                              request.get_full_path(),
                              request.user)
  p['form'] = f
  return render_to_response(request, 'message/info.html', p)

@club_enabled_required
@pm_enabled_required
def sent(request, starting_index):
  pass
